#include <algorithm>
#include <vector>
#include <cmath>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <set>
#include <map>
#include <cstdlib>
#define LL long long
#define ldb long double
const ldb eps = 1e-9;
const ldb pi = fabsl(atan2(0.0, -1.0));
const int INF = 0x7f7f7f7f;
const LL LINF = 1ll << 60;
const ldb LDINF = 1e42;
#define nextLine() {int c = 0; while ((c = getchar()) != 10 && c != EOF);}
#define fill(a, c) memset(a, c, sizeof a)
#define debug(a) cerr << #a << " " << a << "\n";
#define addEdge(a, b) next[edges] = first[a]; first[a] = edges; end[edges] = b; edges++;
using namespace std;

LL l, r;
int t;
const int X = 31;
const int MOD = 38707200;
LL dp[MOD];
int a[20];
int pre[1 << 8][9][8][7][5];
int n;


void Load()
{
	cin >> l >> r;
}

int check(int mask, int m9, int m8, int m7, int m5)
{
	int & res = pre[mask][m9][m8][m7][m5];
	if (res != -1) return res;
	int i;
	res = 1;
	for (i = 0; res && i < 8; i++)
	{
		if ((mask & (1 << i)) != 0)
		{
			i += 2;
			if (i == 2) if (m8 % 2 != 0) res = 0;	
			if (i == 3) if (m9 % 3 != 0) res = 0;
			if (i == 4) if (m8 % 4 != 0) res = 0;
			if (i == 5) if (m5 != 0) res = 0;
			if (i == 6) if (m8 % 2 != 0 || m9 % 3 != 0) res = 0;
			if (i == 7) if (m7 != 0) res = 0;
			if (i == 8) if (m8 != 0) res = 0;
			if (i == 9) if (m9 != 0) res = 0;
		}
	}
	return res;
}

int hash(int i, int mask, int m9, int m8, int m7, int m5, int less)
{
	int h = less;
	h = (h * X + m5) % MOD;
	h = (h * X + m7) % MOD;
	h = (h * X + m8) % MOD;
	h = (h * X + m9) % MOD;
	h = (h * X + mask) % MOD;
	h = (h * X + i) % MOD;
	return h;
}

LL f(int i, int mask, int m9, int m8, int m7, int m5, int less)
{
	LL &res = dp[hash(i, mask, m9, m8, m7, m5, less)];
	if (i == n)
	{
		if (less == 2) return 0;
		else
		{
			if (check(mask, m9, m8, m7, m5)) return 1;	
			else return 0;
		}
	}
	if (res != -1) return res;
	res = 0;
	if (check(mask, m9, m8, m7, m5))
		res++;
	int nless, nmask, nm9, nm8,nm7,nm5;
	int j;
	for (j = 0; j < 9; j++)
	{
		nless = less;
		nmask = mask;
		if (less == 0 && j < a[i]) nless = 1;
		if (less == 0 && j > a[i]) nless = 2;

		if (j >= 2)
			nmask = mask | (1 << (j - 2));
		nm9 = (m9 * 10 + j) % 9;
		nm8 = (m8 * 10 + j) % 8;
		nm7 = (m7 * 10 + j) % 7;
		nm5 = (m5 * 10 + j) % 5;
		res += f(i + 1, nmask, nm9, nm8, nm7, nm5, nless);
	}
	return res;
}

LL count(LL N)
{
	if (N == 0) return 0;
	n = 0;
	while (N > 0)
	{
		a[n++] = N % 10;
		N /= 10;
	}
	reverse(a, a + n);
	memset(dp, 0xFF, sizeof dp);
	LL res = 0;
	int nless, nmask;
	int i;
	for (i = 1; i <= 9; i++)
	{
		if (i < a[0]) nless = 1;
		else if (i == a[0]) nless = 0;
		else nless = 2;
		nmask = 0;
		if (i >= 2) nmask = (1 << (i - 2));
		res += f(1, nmask, i % 9, i % 8, i % 7, i % 5, nless);
	}
	return res;
}

void Solve()
{
	cout << count(r) - count(l - 1) << "\n";
}

int main()
{
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "rt", stdin);
	freopen("output.txt", "wt", stdout);
	#endif
	cin >> t;
	int i;
	memset(pre ,-1, sizeof pre);
	for (i = 0; i < t; i++)
	{
		Load();
		Solve();
	}
	return 0;
}